// 加载图形验证码
$(function () {
    var imgCaptcha = $('.img-captcha');
    imgCaptcha.click(function () {
        imgCaptcha.attr("src", '/account/img_captcha' + '?random=' + Math.random());
        // /account/img_captcha/?random=yyy
    });
});

// 加载短信验证码
$(function () {
    var smsCaptcha = $('.sms-captcha-btn');

    function send_sms() {
        // var telephone = $('input[name="telephone"]');
        var telephone = $("#telephone").val();
        // var telephone = document.getElementById("telephone").valueOf();
        // alert(telephone)
        $.get({
            'url': '/account/sms_captcha/',
            'data': {'telephone': telephone},
            'success': function (result) {
                // console.log('短信发送成功!')
                // alert(telephone)
                console.log(telephone)
                var count = 10;
                smsCaptcha.addClass('disabled');
                smsCaptcha.unbind('click');
                var timer = setInterval(function () {
                    smsCaptcha.text(count + 's')
                    count--;
                    if (count <= -2) {
                        clearInterval(timer)
                        smsCaptcha.text('发送验证码')
                        smsCaptcha.removeClass('disabled')
                        smsCaptcha.click(send_sms)
                    }
                }, 1000)
            },
            'fail': function (error) {
                console.log(error)
            }
        });
    }

    smsCaptcha.click(send_sms);
})


// 注册功能
$(function () {
    $('.submit-btn').click(function (event) {

        // 用ajax请求一定要禁掉传统的表单发送数据的请求方式
        event.preventDefault();

        // 数据有效性验证
        var telephone = $('input[name="telephone"]').val();
        var username = $('input[name="username"]').val();
        var img_captcha = $('input[name="img_captcha"]').val();
        var sms_captcha = $('input[name="sms_captcha"]').val();
        var password1 = $('input[name="password1"]').val();
        var password2 = $('input[name="password2"]').val();

        reg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;//验证手机正则(输入前7位至11位)
        // telephone.match(reg)
        if (!reg.test(telephone)) {
            alert('请输入有效的手机号码!')
            return;
        }

        xfzajax.post({
            'url': '/account/register/',
            'data': {
                'telephone': telephone,
                'username': username,
                'password1': password1,
                'password2': password2,
                'img_captcha': img_captcha,
                'sms_captcha': sms_captcha,
            },
            'success': function (result) {
                // console.log(result);
                if (result['code'] === 200) {
                    window.location = '/';
                } else {
                    // alert(result['message']);
                    var message = result['message'];
                    window.messageBox.showError(message);
                }
            },
            'fail': function (error) {
                // console.log(error);
                window.messageBox.showError(error);
            }
        });

    })


})


/**
 // 点击切换图形验证码
 $(function () {
    var imgCaptcha = $('.img-captcha');
    imgCaptcha.click(function () {
        imgCaptcha.attr("src", '/account/img_captcha' + "?random=" + Math.random());
    });
});


 // 点击发送短信验证码
 $(function () {
    var smsCaptcha = $('.sms-captcha-btn');

    function send_sms() {
        // var telephone = $('input[name="telephone"]');
        // alert(telephone)
        var telephone = $("#telephone").val();
        console.log('coming...');
        console.log(telephone);
        $.get({
            'url': '/account/sms_captcha/',
            'data': {'telephone': telephone},
            'success': function (result) {
                var count = 10;
                smsCaptcha.addClass('disabled');
                smsCaptcha.unbind('click');
                var timer = setInterval(function () {
                    smsCaptcha.text(count);
                    count--;
                    if (count <= 0) {
                        clearInterval(timer);
                        smsCaptcha.text('发送验证码');
                        smsCaptcha.removeClass('disabled');
                        smsCaptcha.click(send_sms);
                    }
                }, 1000);
            },
            'fail': function (error) {
                console.log(error);
            }
        });
    }

    smsCaptcha.click(send_sms);
});
 */